﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;

namespace SpeedRun
{
    public partial class Profile : PhoneApplicationPage
    {
        IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
        public Profile()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Profile_Loaded);

            if (isf.GetFileNames("*.pfk").Length > 0)
            {
                create.IsEnabled = false;
                edit.IsEnabled = true;
            }
            else
            {
                create.IsEnabled = true;
                edit.IsEnabled = false;
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                if (this.NavigationContext.QueryString["id"] != null)
                {
                    String dict = this.NavigationContext.QueryString["id"];
                    if (dict.Equals("1"))
                    {
                        myPivot.SelectedItem = create;
                    }

                    else
                        if (dict.Equals("2"))
                            myPivot.SelectedItem = edit;
                        else
                            myPivot.SelectedItem = settings;
                }
            }
            catch (Exception _e)
            { 
            
            }
            base.OnNavigatedTo(e);
        }



        void Profile_Loaded(object sender, RoutedEventArgs e)
        {
            Settings settings = (App.Current as App).settings;
            location.IsChecked = settings.location;
            music.IsChecked = settings.Music;
            audio.IsChecked = settings.sound;
            report.IsChecked = settings.summary;
            timer.IsChecked = settings.timer;

        }

        private void PivotItem_Loaded(object sender, RoutedEventArgs e)
        {
            if (bodyType.Items.Count > 0)
                return;

            bodyType.Items.Add(BodyType.Aged);
            bodyType.Items.Add(BodyType.Athletic);
            bodyType.Items.Add(BodyType.Fat);
            bodyType.Items.Add(BodyType.Fit);
            bodyType.Items.Add(BodyType.Muscular);
            bodyType.Items.Add(BodyType.Slim);
            sexBox.Items.Add(Gender.male);
            sexBox.Items.Add(Gender.female);
            newsexBox.ItemsSource = sexBox.Items.ToList();
            newbodyType.ItemsSource = bodyType.Items.ToList();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            User currentUser = new User();
            currentUser.Name = name.Text.Trim();
            currentUser.Age = float.Parse(age.Text);
            currentUser.type = (BodyType)bodyType.SelectedItem;
            currentUser.Weight = float.Parse(weight.Text);
            currentUser.gender = (Gender)sexBox.SelectedItem;
            User.Save(currentUser);
            MessageBox.Show("Changes Saved!");
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            User currentUser = new User();
            currentUser.Name = newname.Text.Trim();
            currentUser.Age = float.Parse(newage.Text);
            currentUser.type = (BodyType)newbodyType.SelectedItem;
            currentUser.Weight = float.Parse(newweight.Text);
            currentUser.gender = (Gender)newsexBox.SelectedItem;
            User.Save(currentUser);
            MessageBox.Show("Changes Saved!");
        }

        private void PivotItem_Loaded_1(object sender, RoutedEventArgs e)
        {
            System.Xml.Serialization.XmlSerializer xmlS = new System.Xml.Serialization.XmlSerializer(typeof(User));
            try
            {
                User user = User.UserRetrive();
                newname.Text = user.Name;
                newage.Text = user.Age.ToString();
                newweight.Text = user.Weight.ToString();
                newbodyType.SelectedItem = user.type;
                newsexBox.SelectedItem = user.gender;
            }
            catch (Exception _e)
            { }
        }

        private void profileSelector_Loaded(object sender, RoutedEventArgs e)
        {
             
        }

        private void timer_Checked(object sender, RoutedEventArgs e)
        {
            Settings settings = new Settings();
            settings.location = location.IsChecked.Value;
            settings.Music = music.IsChecked.Value;
            settings.sound = audio.IsChecked.Value;
            settings.summary = report.IsChecked.Value;
            settings.timer =  timer.IsChecked.Value;
            Settings.Save(settings);

            (App.Current as App).settings = settings;
        }

        private void music_Checked(object sender, RoutedEventArgs e)
        {

        }
    }



    public class Settings
    {
        public bool location;
        public bool Music;
        public bool timer;
        public bool sound;
        public bool summary;


        public static void Save(Settings settings)
        {
            try
            {
                XmlSerializer xmlS = new XmlSerializer(typeof(Settings));
                using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                { 
                using(IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("settings.set", System.IO.FileMode.OpenOrCreate, 
                    isf))
                    {
                        xmlS.Serialize(isfs, settings);
                        isfs.Close();
                    }
                }

            }
            catch (Exception _e)
            { 
            }
        }

        public static Settings Retrive()
        {
            Settings setting = new Settings();
            try
            {
                XmlSerializer xmlS = new XmlSerializer(typeof(Settings));
                using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("settings.set", System.IO.FileMode.Open,
                        isf))
                    {
                        setting = (Settings)xmlS.Deserialize(isfs);
                        isfs.Close();
                        return setting;
                    }
                }

            }
            catch (Exception _e)
            {
                return setting;
            }
        }
    }
}